package com.zgt.interceptor;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CharsetFilter implements Filter {
	private String encode; // 定义一个属性，用来存放你设置的编码

	public void destroy() {
		encode = null; // destroy方法把属性回收
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		request.setCharacterEncoding(encode);
		response.setCharacterEncoding(encode);

		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		chain.doFilter(request, response);
	}

	public void init(FilterConfig config) throws ServletException {
		encode = config.getInitParameter("encode"); // init方法读入初始化参数并赋值给属性
	}
}